/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.QuestionDTO;

/**
 *
 * @author User
 */
public class QuestionServiceImpl implements IEntity{
Statement stmt=null;
    ResultSet rst=null;
    private QuestionDTO question;
    private List<QuestionDTO> questionList;

    public QuestionDTO GetById(int id) {
        try {
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from question where idQuestion=" + id);
            question.setIdQuestion(id);
            question.setQuestionTitle(rst.getString("QuestionTitle"));
            question.setQuestionDesc(rst.getString("QuestionDesc"));
            question.setIdQuestionGroup(rst.getInt("IdQuestionGroup"));
            Configuration.CloseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(QuestionServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return question;
    }

    public  List<QuestionDTO> GetAll() {
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from question ");
            while (rst.next()) {
                question.setIdQuestion(rst.getInt("idQuestion"));
                question.setQuestionTitle(rst.getString("QuestionTitle"));
                question.setQuestionDesc(rst.getString("QuestionDesc"));
                question.setIdQuestionGroup(rst.getInt("IdQuestionGroup"));
                questionList.add(question);
            }
            
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(QuestionServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return questionList;
    }

    public void Add(Object obj) {
    	
    	QuestionDTO objCast=(QuestionDTO)obj;
    	try {
    		
			stmt =  Configuration.GetStatement();
			stmt.execute("INSERT INTO dbsurvey.question (`idQuestion`, `QuestionTitle`, `QuestionDesc`,IdQuestionGroup) VALUES ("+objCast.getIdQuestion()+","+objCast.getQuestionTitle()+", "+objCast.getQuestionDesc()+", "+objCast.getIdQuestionGroup()+")");
			Configuration.CloseConnection();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Update(Object obj){
    	
        QuestionDTO objCast=(QuestionDTO)obj;
        try {
			stmt = Configuration.GetStatement();
			stmt.execute("update dbsurvey.question set  QuestionTitle="+objCast.getQuestionTitle()+"and QuestionDesc="+objCast.getQuestionDesc()+"and IdQuestionGroup="+objCast.getIdQuestionGroup()+" where idQuestion="+objCast.getIdQuestion());
			Configuration.CloseConnection();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Remove(int id) {

        try {
        	
			stmt = Configuration.GetStatement();
			stmt.execute("delete from question where idQuestion =" + id);
			Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
